import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.Point;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.*;

/**
 * (Controller) Window_Controller,
 * Controller for all the classes.
 */
public class Window_Controller {
	final Startup_Window sw = new Startup_Window(this);
	final Template_Window tw = new Template_Window(this);
	WorldCreator wc = null;
	Animation_Window aw = null;//new Animation_Window(this);
	public BufferedImage torso, head, left_arm, right_arm, left_leg, right_leg;
	
	public Point joint_torso_head, joint_torso_left_arm, joint_torso_right_arm, joint_torso_left_leg, joint_torso_right_leg,
	  joint_head, joint_left_arm, joint_right_arm, joint_left_leg, joint_right_leg; 
	 
	public ImageIcon img;
	
	public Window_Controller() {
	}
	
	/**
	 * Sets the body images in the ImageDB and creates the common_script.rb file.
	 */
	public void create_Figur() {
		ImageDB.images.put("krop", torso);
		ImageDB.images.put("hben", right_leg);
		ImageDB.images.put("vben", left_leg);
		ImageDB.images.put("harm", right_arm);
		ImageDB.images.put("varm", left_arm);
		ImageDB.images.put("hoved", head);
		
		Point joint_head_final = new Point(),
		  joint_left_arm_final = new Point(),
		  joint_right_arm_final = new Point(),
		  joint_left_leg_final = new Point(),
		  joint_right_leg_final = new Point();
			
		joint_head_final.x = (joint_head.x - joint_torso_head.x);
		joint_head_final.y = (joint_head.y - joint_torso_head.y);
		
		joint_left_arm_final.x = (joint_left_arm.x - joint_torso_left_arm.x);
		joint_left_arm_final.y = (joint_left_arm.y - joint_torso_left_arm.y);
		 
		joint_right_arm_final.x = (joint_right_arm.x - joint_torso_right_arm.x);
		joint_right_arm_final.y = (joint_right_arm.y - joint_torso_right_arm.y);
		
		joint_left_leg_final.x = (joint_left_leg.x - joint_torso_left_leg.x);
		joint_left_leg_final.y = (joint_left_leg.y - joint_torso_left_leg.y);
		
		joint_right_leg_final.x = (joint_right_leg.x - joint_torso_right_leg.x);
		joint_right_leg_final.y = (joint_right_leg.y - joint_torso_right_leg.y);
		
		try {
			BufferedReader in = new BufferedReader(new FileReader(Main.srcPath + "common2.rb")); 
			
			String str, script = "";
    		while ((str = in.readLine()) != null) {
    			script = script + str + "\n";
    			if (str.contains("#insert_parts")) {
    				script += "led = Led.new\n";
    				if (joint_right_leg_final.x < 0) {script += "led.opx = " + Math.abs(joint_right_leg_final.x) + "\n" + "led.nedx = 0\n";} else {
						 							  script += "led.opx = 0\n" + "led.nedx = " + Math.abs(joint_right_leg_final.x) + "\n";}
    				if (joint_right_leg_final.y < 0) {script += "led.opy = " + Math.abs(joint_right_leg_final.y) + "\n" + "led.nedy = 0\n";} else {
						 							  script += "led.opy = 0\n" + "led.nedy = " + Math.abs(joint_right_leg_final.y) + "\n";}
    				script += "led.del = HBen.new\n" +
    						  "self.led[\"hben\"] = led\n\n" +
    						 
    						  "led = Led.new\n";
    				if (joint_left_leg_final.x < 0) {script += "led.opx = " + Math.abs(joint_left_leg_final.x) + "\n" + "led.nedx = 0\n";} else {
    												 script += "led.opx = 0\n" + "led.nedx = " + Math.abs(joint_left_leg_final.x) + "\n";}
    				if (joint_left_leg_final.y < 0) {script += "led.opy = " + Math.abs(joint_left_leg_final.y) + "\n" + "led.nedy = 0\n";} else {
    												 script += "led.opy = 0\n" + "led.nedy = " + Math.abs(joint_left_leg_final.y) + "\n";}
    				script += "led.del = VBen.new\n" +
    						  "self.led[\"vben\"] = led\n\n" +
    						  
    						  "led = Led.new\n";
    				if (joint_right_arm_final.x < 0) {script += "led.opx = " + Math.abs(joint_right_arm_final.x) + "\n" + "led.nedx = 0\n";} else {
						 							  script += "led.opx = 0\n" + "led.nedx = " + Math.abs(joint_right_arm_final.x) + "\n";}
    				if (joint_right_arm_final.y < 0) {script += "led.opy = " + Math.abs(joint_right_arm_final.y) + "\n" + "led.nedy = 0\n";} else {
						 							  script += "led.opy = 0\n" + "led.nedy = " + Math.abs(joint_right_arm_final.y) + "\n";}
    				script += "led.del = HArm.new\n" +
    						  "self.led[\"harm\"] = led\n\n" +
    						  
    						  "led = Led.new\n";
    				if (joint_left_arm_final.x < 0) {script += "led.opx = " + Math.abs(joint_left_arm_final.x) + "\n" + "led.nedx = 0\n";} else {
						 							  script += "led.opx = 0\n" + "led.nedx = " + Math.abs(joint_left_arm_final.x) + "\n";}
    				if (joint_left_arm_final.y < 0) {script += "led.opy = " + Math.abs(joint_left_arm_final.y) + "\n" + "led.nedy = 0\n";} else {
						 							  script += "led.opy = 0\n" + "led.nedy = " + Math.abs(joint_left_arm_final.y) + "\n";}
    				script += "led.del = VArm.new\n" +
    						  "self.led[\"varm\"] = led\n\n" +
    						  
   							  "led = Led.new\n";
   					if (joint_head_final.x < 0) {script += "led.opx = " + Math.abs(joint_head_final.x) + "\n" + "led.nedx = 0\n";} else {
	 							 					  script += "led.opx = 0\n" + "led.nedx = " + Math.abs(joint_head_final.x) + "\n";}
    				if (joint_head_final.y < 0) {script += "led.opy = " + Math.abs(joint_head_final.y) + "\n" + "led.nedy = 0\n";} else {
	 							 					  script += "led.opy = 0\n" + "led.nedy = " + Math.abs(joint_head_final.y) + "\n";}
    				script += "led.del = Hoved.new\n" +
    						  "self.led[\"Hoved\"] = led\n";
    			}
    		}
    		
    		BufferedWriter out = new BufferedWriter(new FileWriter(Main.srcPath + "common_script.rb"));
			out.write(script);
			out.flush();
			out.close();
		} catch (IOException e) {
		}
	}
	
	public void create_Load_Window() {
		Load_Window load_w = new Load_Window(this);
		load_w.setVisible(true);
	}
	
	public void create_Save_Window(Editor ed) {
		Save_Window save_w = new Save_Window(this, ed);
		save_w.setVisible(true);
	}
	
	/**
	 * Shows the WorldCreator frame.
	 * @param prescript
	 */
	public void show_WorldCreator(String prescript) {
		wc = new WorldCreator(prescript, this);
		wc.setVisible(true);
	}
	
	/**
	 * Shows the WorldCreator frame.
	 */
	public void show_WorldCreator() {
		if (wc == null)
			System.exit(-1);
		else
			wc.setVisible(true);
	}
	
	/**
	 * Hides the WorldCreator frame.
	 */
	public void hide_WorldCreator() {
		if (wc == null)
			System.exit(-1);
		else
			wc.setVisible(false);
	}

	/**
	 * Shows the Animation_Window frame.
	 */
	public void show_Animation_Window() {
		aw.setVisible(true);
	}
	
	/**
	 * Hides the Animation_Window frame.
	 */
	public void hide_Animation_Window() {
		aw.setVisible(false);
	}
	
	/**
	 * Shows the Startup_Window frame.
	 */
	public void show_Startup_Window() {
		sw.setVisible(true);
	}
	
	/**
	 * Hides the Startup_Window frame.
	 */
	public void hide_Startup_Window() {
		sw.setVisible(false);
	}
	
	/**
	 * Shows the Template_Window frame.
	 */
	public void show_Template_Window() {
		tw.setVisible(true);
	}
	
	/**
	 * Hides the Template_Window frame.
	 */
	public void hide_Template_Window() {
		tw.setVisible(false);
	}
	
	/**
	 * Changes the image path to an image and calls change_Template_Window_img(ImageIcon)
	 * @param imgPath					(imagePath) String. 
	 */
	public void change_Template_Window_img(String imgPath) {
		ImageIcon img = new ImageIcon(Toolkit.getDefaultToolkit().getImage(imgPath));
		change_Template_Window_img(img);
	}
	
	/**
	 * Changes the image on the Template_Window to the new one.
	 * @param img						(image) ImageIcon.
	 */
	public void change_Template_Window_img(ImageIcon img) {
		tw.img = img;
		
		int imgWidth = tw.img.getIconWidth();
		int imgHeight = tw.img.getIconHeight();
		
		while (imgWidth > 700 && imgHeight > 700) {
			imgWidth -= 1;
			imgHeight -= 1;
		}
		
		tw.img = new ImageIcon(tw.img.getImage().getScaledInstance(imgWidth, imgHeight, 0));
		
		Dimension prefDim = new Dimension(imgWidth,imgHeight);
		if (imgWidth > Toolkit.getDefaultToolkit().getScreenSize().width - 400) {
			imgWidth = Toolkit.getDefaultToolkit().getScreenSize().width - 400;
		}
		if (imgHeight > Toolkit.getDefaultToolkit().getScreenSize().height - 400) {
			imgHeight = Toolkit.getDefaultToolkit().getScreenSize().height - 400;
			prefDim.height = imgHeight;
		}
		
		JLabel new_jLabel = new JLabel(tw.img);
		new_jLabel.setPreferredSize(prefDim);
		
		tw.setFrameSize(imgWidth,imgHeight);
		
		tw.top_JPanel.remove(1);
		tw.top_JPanel.add(new_jLabel);
		tw.top_JPanel.repaint();
		
		tw.repaint();
	}
	
	/**
	 * Shows the FileChooser frame.
	 */
	public void show_FileChooser() {
		final ChooseFile_Window cfw = new ChooseFile_Window(this);
		cfw.setVisible(true);
	}
	
	/**
	 * Shows the Mark_Window frame.
	 * @param img_icon					(imageicon) ImageIcon.
	 */
	public void show_Mark_Window(ImageIcon img_icon) {
		final Mark_Window mw = new Mark_Window(this);
		mw.setVisible(true);
		mw.setImage(img_icon, this);
	}
	
	/**
	 * Shows the Mark_Joints_Window frame.
	 * @param torso_img
	 * @param head_img
	 * @param left_arm_img
	 * @param right_arm_img
	 * @param left_leg_img
	 * @param right_leg_img
	 */
	public void show_Mark_Joints_Window(BufferedImage torso_img,
										BufferedImage head_img,
										BufferedImage left_arm_img,
										BufferedImage right_arm_img,
										BufferedImage left_leg_img,
										BufferedImage right_leg_img) {
		final Mark_Joints_Window mjw = new Mark_Joints_Window(this);
		mjw.setVisible(true);
		mjw.setImages(torso_img,
					  head_img,
					  left_arm_img,
					  right_arm_img,
					  left_leg_img,
					  right_leg_img,
					  this);
	}
	
	/**
	 * Closes the Mark_Window frame.
	 * @param mw				(Mark_Window) Mark_Window
	 */
	public void close_Mark_Window(Mark_Window mw) {
		mw.dispose();
	}
	
	/**
	 * Closes the Mark_Joints_Window frame.
	 * @param mjw				(Mark_Joints_Window) Mark_Joints_Window
	 */
	public void close_Mark_Joints_Window(Mark_Joints_Window mjw) {
		mjw.dispose();
	}
}
